#pragma once
#include<iostream>
#include<pthread.h>

namespace MuteModule
{
    class Mutex 
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_mutex,nullptr);//初始化锁
        }
        void Lock()
        {
            int n = pthread_mutex_lock(&_mutex);//申请锁
            if(n == 0)
            {
                //std::cout << "加锁成功" << std::endl;
            }
            (void) n;
        }
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_mutex);//解锁
            if(n == 0)
            {
                //std::cout << "解锁成功" << std::endl;
            }
            (void) n;
        }
        pthread_mutex_t* Get()
        {
            return &_mutex;
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);//释放锁
            //std::cout << "hhh" << std::endl;
        }
    private:
        pthread_mutex_t _mutex;
    };
    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex)
        :_mutex(mutex)
        {
            _mutex.Lock();
           // std::cout << "加锁成功" << std::endl;
        }
        
        ~LockGuard()
        {
            _mutex.Unlock();
           // std::cout << "解锁成功" << std::endl;

        }
    private:
        Mutex& _mutex;//必须是引用不然是给本线程的拷贝上锁无意义
    };
}